iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 3
0

CNN如何實現

今天我們就要來實際來實作一個CNN!

前置作業

我們使用Colab來當作我們的實作平台,並使用Keras來完成。

資料集

fashion_mnist,為Keras內建的資料集
訓練集為60,000 張28x28 像素灰度圖像,測試集為10,000 同規格圖像,總共10 類時尚物品標籤。該數據集可以用作MNIST 的直接替代品。類別標籤是:
類別 描述 中文
0 T-shirt/top T卹/上衣
1 Trouser 褲子
2 Pullover 套頭衫
3 Dress 連衣裙
4 Coat 外套
5 Sandal 涼鞋
6 Shirt 襯衫
7 Sneaker 運動鞋
8 Bag 背包
9 Ankle boot 短靴

Code

前置處理

讀取資料集後,把數值都scale到0~1之間。

from keras.layers import Input, Dense, Conv1D, Conv2D, MaxPooling1D,\
    MaxPooling2D, UpSampling1D, UpSampling2D, Dropout, Lambda, Convolution2D,\
    Reshape, Activation, Flatten, add, concatenate, Subtract, BatchNormalization
from keras.models import Model, Sequential
from keras.datasets import fashion_mnist
import numpy as np
import keras

nb_classes=10
nb_epoch=25
batch_size=150

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], x_train.shape[2], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], x_test.shape[2], 1))

model

依照自己的想法建立的model,可以自己決定層數等

tag = 'CNN0825_fashion_mnist_{}'.format(0)
h5_weight_path = os.path.join(WEIGHT_DIR, './' + tag + '.h5')

input_shape=(28,28,1)

input = Input(input_shape, name='input')

layer=Conv2D(32, kernel_size=(2, 2), activation='relu', padding='same')(input)
layer=BatchNormalization()(layer)
layer=Conv2D(32, kernel_size=(2, 2), activation='relu',padding='same')(layer)
layer=BatchNormalization()(layer)

layer=Conv2D(64, kernel_size=(2, 2), activation='relu', padding='same')(layer)
layer=BatchNormalization()(layer)
layer=Conv2D(64, kernel_size=(2, 2), activation='relu', padding='same')(layer)
layer=BatchNormalization()(layer)
layer=MaxPooling2D(pool_size=(2, 2))(layer)

layer=Conv2D(128, kernel_size=(2, 2), activation='relu', padding='same')(layer)
layer=BatchNormalization()(layer)
layer=Conv2D(128, kernel_size=(2, 2), activation='relu', padding='same')(layer)
layer=BatchNormalization()(layer)
layer=MaxPooling2D(pool_size=(2, 2))(layer)

layer=Conv2D(256, kernel_size=(2, 2), activation='relu', padding='same')(layer)
layer=BatchNormalization()(layer)
layer=Conv2D(256, kernel_size=(2, 2), activation='relu', padding='same')(layer)
layer=BatchNormalization()(layer)
layer=MaxPooling2D(pool_size=(2, 2))(layer)


layer = Dropout(0.5)(layer)
layer = Flatten(name='flatten')(layer)
output = Dense(nb_classes, name="Dense_10nb", activation='softmax')(layer)

model = Model(inputs=[input], outputs=[output])

model.compile(loss='sparse_categorical_crossentropy',optimizer=keras.optimizers.Adam(lr=0.0001,decay=1e-6),metrics = ['accuracy'])
model.summary()

訓練

model.fit(x=x_train, y=y_train,batch_size=batch_size, epochs=nb_epoch,verbose=1,validation_data=(x_test, y_test))
#儲存權重
model.save_weights(os.path.join(WEIGHT_DIR, tag + ".h5"))

結果

model.load_weights(h5_weight_path)
score, acc = model.evaluate([x_test], [y_test])
print(' score= ', score, " acc= ", acc)
predict = model.predict(x_test)

訓練結果

Epoch 1/25
400/400 [==============================] - 35s 89ms/step - loss: 1.0272 - accuracy: 0.7235 - val_loss: 2.9579 - val_accuracy: 0.2935
...
Epoch 25/25
400/400 [==============================] - 35s 87ms/step - loss: 0.0471 - accuracy: 0.9838 - val_loss: 0.3233 - val_accuracy: 0.9129

測試結果

313/313 [==============================] - 3s 8ms/step - loss: 0.3233 - accuracy: 0.9129
 score=  0.32327505946159363  acc=  0.9128999710083008

結論

今天使用了Keras實作了CNN,並且正確率有91.29%。

參考資料

Keras常用資料集
Sequential 模型API


上一篇
Day 2 CNN是新聞頻道呀~
下一篇
Day 4 GAN是一種X話吧?
系列文
Machine Learning與軟工是否搞錯了什麼?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言